# -*- coding:utf-8 -*-
from flask import request
from flask_restful import Resource, marshal_with, reqparse
from sqlalchemy import not_

from apps.models import Kind, News
from apps.apis.reslut import Result
from apps.apis.fields import home_result, kind_result, detail_result


class HomeApi(Resource):
    @marshal_with(home_result)
    def get(self):
        # 查询所有种类
        kind_list = Kind.query.all()
        # 热门新闻
        hot_news = News.query.order_by("-click_num")[0:4]
        # 新闻
        news = Kind.query.filter_by(type="新闻").first()
        news_list = News.query.filter_by(type_id=news.type_id).order_by("-create_time")[0:9]
        # 娱乐新闻
        recreation = Kind.query.filter_by(type="娱乐").first()
        recreation_list = News.query.filter_by(type_id=recreation.type_id).order_by("-create_time")[0:9]
        # 体育新闻
        sports = Kind.query.filter_by(type="体育").first()
        sports_list = News.query.filter_by(type_id=sports.type_id).order_by("-create_time")[0:9]
        # 图片新闻
        img_news = News.query.filter(not_(News.img_url == '0'))[0:5]
        # 财经新闻
        finance = Kind.query.filter_by(type="财经").first()
        finance_list = News.query.filter_by(type_id=finance.type_id).order_by("-create_time")[0:9]
        # 科技新闻
        science = Kind.query.filter_by(type="科技").first()
        science_list = News.query.filter_by(type_id=science.type_id).order_by("-create_time")[0:9]
        # 汽车新闻
        car = Kind.query.filter_by(type="汽车").first()
        car_list = News.query.filter_by(type_id=science.type_id).order_by("-create_time")[0:9]
        # 女人新闻
        woman = Kind.query.filter_by(type="女人").first()
        woman_list = News.query.filter_by(type_id=woman.type_id).order_by("-create_time")[0:9]
        # 房产新闻
        house = Kind.query.filter_by(type="房产").first()
        house_list = News.query.filter_by(type_id=house.type_id).order_by("-create_time")[0:9]
        # 旅游新闻
        travel = Kind.query.filter_by(type="旅游").first()
        travel_list = News.query.filter_by(type_id=travel.type_id).order_by("-create_time")[0:9]
        # 最新消息
        now_news = News.query.order_by("-create_time")[0:9]
        # 教育新闻
        education = Kind.query.filter_by(type="教育").first()
        education_list = News.query.filter_by(type_id=education.type_id).order_by("-create_time")[0:12]
        # 公益新闻
        public = Kind.query.filter_by(type="公益").first()
        public_list = News.query.filter_by(type_id=public.type_id).order_by("-create_time")[0:12]
        # 校园新闻
        school = Kind.query.filter_by(type="校园").first()
        school_list = News.query.filter_by(type_id=school.type_id).order_by("-create_time")[0:9]
        # 轮播新闻
        banner_list = News.query.filter(not_(News.img_url == '0')).order_by("-create_time")[0:3]
        data = {
            'kind': kind_list,
            'news_div': {
                'hot_news': hot_news,
                'now_news': now_news,
                'img_news': img_news,
                'banner_news': banner_list,
                'kind_news': {
                    'news_news': {
                        'news': news,
                        'news_list': news_list,
                    },
                    'recreation_news': {
                        'recreation': recreation,
                        'recreation_list': recreation_list,
                    },
                    'sport_news': {
                        'sport': sports,
                        'sport_list': sports_list,
                    },
                    'finance_news': {
                        'finance': finance,
                        'finance_list': finance_list,
                    },
                    'science_news': {
                        'science': science,
                        'science_list': science_list,
                    },
                    'car_news': {
                        'car': car,
                        'car_list': car_list,
                    },
                    'woman_news': {
                        'woman': woman,
                        'woman_list': woman_list,
                    },
                    'house_news': {
                        'house': house,
                        'house_list': house_list,
                    },
                    'travel_news': {
                        'travel': travel,
                        'travel_list': travel_list,
                    },
                    'education_news': {
                        'education': education,
                        'education_list': education_list,
                    },
                    'public_news': {
                        'public': public,
                        'public_list': public_list,
                    },
                    'school_news': {
                        'school': school,
                        'school_list': school_list,
                    },
                }
            }
        }
        return Result.get_success_result_obj(data)


class CatApi(Resource):

    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('type', type=int, default=1)
        self.parser.add_argument('page', type=int, default=1)
        self.parser.add_argument('size', type=int, default=20)

    @marshal_with(kind_result)
    def get(self):
        args = self.parser.parse_args()
        type_id = args.get('type')
        page = args.get('page')
        size = args.get('size')
        # 查询所有种类
        kind_list = Kind.query.all()
        # 查询类别ID为type_id的新闻数据
        type = Kind.query.filter_by(type_id=type_id).first()
        news_list = News.query.filter_by(type_id=type.type_id).order_by("-create_time").paginate(page=page,
                                                                                                 per_page=size,
                                                                                                 error_out=False).items
        hot_news = News.query.filter_by(type_id=type.type_id).order_by("-click_num").limit(10).all()
        data = {
            'kind': kind_list,
            'news': {
                'type': type,
                'hot_news': hot_news,
                'news_list': news_list,
                'page': page
            }
        }
        return Result.get_success_result_obj(data)


class DetailApi(Resource):

    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('new', type=str, default='00276ef2301c93c9b8e7d9effb63b5b8')

    @marshal_with(detail_result)
    def get(self):
        args = self.parser.parse_args()
        new_id = args.get('new')
        kind_list = Kind.query.all()
        new = News.query.filter_by(hash_id=new_id).first()
        new_type = Kind.query.filter_by(type_id=new.type_id).first()
        data = {
            'kind': kind_list,
            'new': new,
            'new_type': new_type
        }
        return Result.get_success_result_obj(data)
